﻿#ifndef TIMEMANAGE_H
#define TIMEMANAGE_H
#include <ProcessInfoStartuct.h>
#include <QApplication>
#include <QMutexLocker>
#include <QProcess>
#include <QPair>
#include <QSemaphore>
#include <SettingPage.h>
#include <QSharedPointer>
#include "TimeManage_global.h"
class ThreadCin;
class CheckOptionAttributeSetting;

class TIMEMANAGE_EXPORT TimeManage : public QApplication {
Q_OBJECT;
public:
	// 程序运行结束
	void finished(int exitCode
		, QProcess::ExitStatus exitStatus);
	// 从程序中获取输出
	void readyReadStandardOutput( );
	void writeFileInfo(QString filetarget
		, QString writeCon
		, QSemaphore* qSemaphore);
	// 启动
	void started( );
	// 状态被改变
	void stateChanged(QProcess::ProcessState newState);
	// 错误退出
	void errorOccurred(QProcess::ProcessError error);
public:
	// 获取输入
	void getCin(QString* str
		, QSemaphore* qSemaphore);
private:
private:
	// json 配置
	QJsonDocument* userJsonDocument = Q_NULLPTR;
	// 配置文件路径
	int userOutOption = 0;
	quint64 debugNumber = 0;
	QString currentExeName;
	// 最小调用周期
	// -1 为不调用
	qint64 minCallMs = -1;
	// 最小调用周期
	QTime minTimeOut;
	// 所有进程
	QHash<QString, ProcessInfoStartuct*> processMap;
	// 正确的进程任务
	// 基于配置的出现允许运行的进程
	QVector<ProcessInfoStartuct*> processRun;
	// 时间间隔
	QHash<ProcessInfoStartuct*, QTime*> processTimeIntervalMap;
	// 时间间隔存储器
	QVector<QTime*> processTimeIntervalMemory;
	// QHash<QTime*, QTime*> 指定运行时间段
	QMultiHash<ProcessInfoStartuct*, QPair<QTime*, QTime*>*> processTimeMap;
	// 时间管理内存
	QVector<QPair<QTime*, QTime*>*> timeDataMemory;
	// 开始进程
	QHash<ProcessInfoStartuct*, QProcess::ProcessState> runProcess;
	// 错误进程
	QHash<ProcessInfoStartuct*, QProcess::ProcessError> procesProcessErrorMap;
	// 参数选项
	QHash<QString, QVector<QString>> parameter;
	// 检查毫秒数
	int startMinNsec = 60000;
	// 获取到的配置文件
	QVector<QString> files;
	// 配置获取
	CheckOptionAttributeSetting* xmlSet = Q_NULLPTR;
	// 程序选项
	QSharedPointer<QHash<QString, QSharedPointer<QVector<QString>>>> defaultFileOption;
	// 输入获取锁
	QMutex* ioMutex = Q_NULLPTR;
	// 线程运行队列锁
	QMutex* processRunMutex = Q_NULLPTR;
	// 输入线程
	ThreadCin* threadCin = Q_NULLPTR;
	// settingObj
	SettingPage* settingInstance(const QString* file = Q_NULLPTR
		, QHash<QString, ProcessInfoStartuct*>* processMap = Q_NULLPTR
		, int* readResult = Q_NULLPTR);
	QTimer* timer = Q_NULLPTR;
	QHash<QTimer*, ProcessInfoStartuct*> timerMap;
	// 文件写入 信号量
	QSemaphore writeFilesSemaphore;
public:
	TimeManage(int& argc
		, char** argv
		, int i = ApplicationFlags);
	void creatorProcessInfoStartuctObj(ProcessInfoStartuct* processObj);
	~TimeManage( );
	// 设置配置文件
	int appendSettingFile(const QString& fileName);
	void resetMaiProcessCallInfos( );
	int runMainProcess( );
	void processFirstStart( );
	void startRunProcess(QTime& currentDateTime
		, ProcessInfoStartuct* processInfoStartuct
		, QTime* time
		, int& seepTime
		, QMutexLocker& locker);
	void TimerTimeOut( );
	const char* getState(QProcess::ProcessState newState);
	const char* getState(QProcess::ExitStatus exitStatus);
	const char* getState(QProcess::ProcessError error);
};
#endif // TIMEMANAGE_H
